<?php
include("frmCookie.php");
include("includes/functions.php");

$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);

$idUsuario = $_SESSION['idUsuario'];
$idModelo = $_POST['idModelo'];
$comercial = $_POST['comercial'];
$fabricante = $_POST['fabricante'];

makeDirectory(PATH_FOTOS."$idUsuario/");
//makeDirectory("temp/$idUsuario/");

$uploadsDirectory = PATH_FOTOS."$idUsuario/";
//$uploadsDirectory = "temp/$idUsuario/";

$uploadForm = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'modelosUploadFotos.php?iOpcion=2&comercial='.$comercial.'&fabricante='.$fabricante.'&idModelo='.$idModelo;

$uploadSuccess = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'modelosUploadFotos.php?iOpcion=2&comercial='.$comercial.'&fabricante='.$fabricante.'&idModelo='.$idModelo;

$fieldname = 'file';

$errors = array(1 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');

$active_keys = array();
foreach($_FILES[$fieldname]['name'] as $key => $filename)
{
	if(!empty($filename))
	{
		$active_keys[] = $key;
	}
}

count($active_keys)
	or error('No files were uploaded', $uploadForm);
		
foreach($active_keys as $key)
{
	($_FILES[$fieldname]['error'][$key] == 0)
		or error($_FILES[$fieldname]['tmp_name'][$key].': '.$errors[$_FILES[$fieldname]['error'][$key]], $uploadForm);
}
	
foreach($active_keys as $key)
{
	@is_uploaded_file($_FILES[$fieldname]['tmp_name'][$key])
		or error($_FILES[$fieldname]['tmp_name'][$key].' not an HTTP upload', $uploadForm);
}
	
foreach($active_keys as $key)
{
	@getimagesize($_FILES[$fieldname]['tmp_name'][$key])
		or error($_FILES[$fieldname]['tmp_name'][$key].' not an image', $uploadForm);
}
	
foreach($active_keys as $key)
{
	$uploadFilename[$key] = $uploadsDirectory.$_FILES[$fieldname]['name'][$key];
}

foreach($active_keys as $key)
{
	@move_uploaded_file($_FILES[$fieldname]['tmp_name'][$key], $uploadFilename[$key])
		or error('receiving directory insuffiecient permission', $uploadForm);
}
	
header('Location: ' . $uploadSuccess);

function error($error, $location, $seconds = 5)
{
	header("Refresh: $seconds; URL=\"$location\"");
	echo '<html>'.
	'	<head>'.
	'		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'.
	'		<link rel="stylesheet" type="text/css" href="stylesheet/style.css">'.
	'		<title>Upload error</title>'.
	'	</head>'.
	'	<body>'.
	'<table cellpadding="0" cellspacing="0" align="center" width="100%">
        <tr>
            <td align="center">';
     echo '
                <table cellpadding="0" cellspacing="0" align="center">
                <tr>
                    <td>'.
	'		<h1>Agregar Imagen ha fallado</h1>'.
	'		<p class="error">Un error ha ocurrido: '.
	'		<span class="red">' . $error . '...</span>'.
	'	 	El formulario se volverá a cargar en 5 segundos. Por favor espere.</p>'.
	'</td>
					</tr>
				</table>
            </td>
        </tr>
    </table>';
    
	echo '</body></html>';
	exit;
}
?>